# ifndef __Menu_H_
# define __Menu_H_

# include "fvector.h"

void InitMenu ();
void UpdateMenu ();
void UpdateImportExportMenu ();

void PopupMenu (HMENU _hMenu);

bool IsSeparator (HMENU _hMenu, int _nPos);

void DeleteDuplicatedMenuSeparators (HMENU _hMenu);
void DeleteParentMenu  (HMENU _hMenu, UINT _uiCmd);
void DisableParentMenu (HMENU _hMenu, UINT _uiCmd);

class CMenuHolder {
	// Disable copy semantics.
	operator = (const CMenuHolder &);
	CMenuHolder (const CMenuHolder &);

  public:
	CMenuHolder (HMENU _hMenu) : m_hMenu (_hMenu) {}
	~CMenuHolder () {
		if (m_hMenu)
			::DestroyMenu (m_hMenu);
	}
  private:
	HMENU m_hMenu;
};

class CMenuBase {
  public:
	CMenuBase ();
	~CMenuBase ();

	int ShowAsPopup (HWND _hwndParent, RECT & _rectExclude) const;

	void AppendItem (WORD _wCmdId, const char * _strItem);
	void RemoveItem     (WORD _wCmdId);

	void EnableItem     (WORD _wCmdId, bool _bEnable);
	void CheckItem      (WORD _wCmdId, bool _bCheck);
	void SetDefaultItem (WORD _wCmdId);

	operator HMENU () const {return m_hMenu;}

	HMENU GetMenu    () const {return m_hMenu;}
	HWND  GetToolbar () const {return m_hwndToolbar;}

	// Handler of WM_MEASUREITEM.
	void OnMenuMeasureItem (LPARAM _lParam) const;
	// Handler of WM_DRAWITEM.
	void OnMenuDrawItem    (LPARAM _lParam) const;

	struct menuitem_data_t {
		WORD wCmdId;
		WORD wImage;
	};

//	void SetIcons (UINT _uiIconsBmp, const menuitem_data_t * _pMenuItems, size_t _cMenuItems);
	// Update owner-draw flags for menu items.
	// NOTE: To be invoked after each menu item inserting.
	void UpdateODItems ();

	void RemoveDuplicatedMenuSeparators ();

  protected:
	bool m_bToDestroy;
	bool m_bHorzMenu;
	HMENU m_hMenu;
	HWND  m_hwndToolbar;

	HIMAGELIST m_hilIcons;
	const menuitem_data_t * m_pMenuItems;
	size_t m_cMenuItems;
	size_t _FindImageForCmd (WORD _wCmdId) const;

	fvector<TCHAR *> m_MenuStrings;
	void _DestroyMenuStrings ();

	void _MakeMenuItemsOD (HMENU _hMenu, bool _bHorzMenu);
};

class CPopupMenu : public CMenuBase {
  public:
	CPopupMenu ();
	CPopupMenu (HMENU _hMenu);
};

class CMainMenu : public CMenuBase {
  public:
	void Init (HMENU _hMenu);
	void SetToolbar (HWND _hwndToolbar);
};

extern CMainMenu g_MainMenu;
extern HMENU g_hmenuWTR;

/////////////////////////////

enum {
	// Main submenus.
    smFile      = 0,
	smEdit      = 1,
	smView      = 2,
	smFavorites = 3,
	smTools     = 4,
	smHelp      = 5,

	// 'View' submenus.
	smViewScales = 3,
	smViewLevels = 6,

	// Popup submenus.
	smpObject    = 0,
	smpRaster    = 1,
	smpWTR       = 2,
	smpEditNode  = 3,
	smpEditLine  = 4,
	smpTrim      = 5,
	smpCreate    = 6,
	smpGoogle    = 7,
	smpRoutes    = 8,
};

# endif __Menu_H_